package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Citext;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CitextService;

/**
 * Servlet implementation class CitextServlet
 */
public class CitextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;encoding=UTF-8");
		

		/* 允许跨域的主机地址 */
		response.setHeader("Access-Control-Allow-Origin", "*");  
		/* 允许跨域的请求方法GET, POST, HEAD 等 */
		response.setHeader("Access-Control-Allow-Methods", "*");  
		/* 重新预检验跨域的缓存时间 (s) */
		response.setHeader("Access-Control-Max-Age", "3600");  
		/* 允许跨域的请求头 */
		response.setHeader("Access-Control-Allow-Headers", "*");  
		/* 是否携带cookie */
		response.setHeader("Access-Control-Allow-Credentials", "true");
		
		PrintWriter out = response.getWriter();
		
		
		String modeString = request.getParameter("mode");
		if (modeString == null) {
			out.write("no mode");
			return;
		}
		
		int mode = Integer.parseInt(modeString);
		CitextService citextService = BeanFactory.getBean("citextService");
		
		switch (mode) {
		//###################################################################//
		// 1 增
		case 11:{
			String textString = request.getParameter("text");
			String levelString = request.getParameter("level");
			String whoString = request.getParameter("who");
			String labidString = request.getParameter("labid");
			
			
			if(textString == null || levelString == null || whoString == null || labidString == null) {
				out.write("param lost");
				return;
			}
			
			Citext citext = new Citext();
			citext.setText(textString);
			int level = Integer.parseInt(levelString);
			citext.setLevel(level);
			citext.setWho(whoString);
			int labid = Integer.parseInt(labidString);
			citext.setLabid(labid);
			
			int i = citextService.addCitext(citext);
			out.write("{'state':'"+i+"'}");
			return;
		}
		
		//###################################################################//
		// 2 删
		case 21:{
			String idString = request.getParameter("id");
			if (idString == null) {
				out.write("param lost");
				response.sendError(566, "param lost");
				return;
			}
			
			int id = Integer.parseInt(idString);
			int i = citextService.deleteCitext(id);
			out.write("{'state':'"+i+"'}");
			return;
		}
		
		//###################################################################//
		// 3 改
		
		case 31:{
			String idString = request.getParameter("id");
			String textString = request.getParameter("text");
			String levelString = request.getParameter("level");
			String whoString = request.getParameter("who");
			
			System.out.println(textString);
			
			if(idString == null|| textString == null || levelString == null || whoString == null) {
				out.write("param lost");
				return;
			}
			
			Citext citext = new Citext();
			
			
			//citext的初始化//
			int id = Integer.parseInt(idString); 
			citext.setId(id);
			citext.setText(textString);
			int level = Integer.parseInt(levelString);
			citext.setLevel(level);
			citext.setWho(whoString);
			//citext的初始化结束//
			
			
			int i = citextService.updateCitext(citext);
			out.write("{'state':'"+i+"'}");
			return;
		}
		
		//###################################################################//
		// 4 查
		case 41:{
			String labidString = request.getParameter("labid");
			
			if (labidString == null) {
				out.write("param lost");
				return;
			}
			
			int labid = Integer.parseInt(labidString);
			
			List<Citext> citextList = citextService.findCitextByLabid(labid);
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append('[');
			for(Citext myCitext:citextList) {
				stringBuilder.append(myCitext.toJsonString());
				stringBuilder.append(',');
			}
			stringBuilder.append(']');
			
			out.write(stringBuilder.toString());
			return;
		}
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
